$(function () {

    // 固定专业/科目
    $('#myAffix').affix({
        offset: {
            top: 296,
            bottom: function () {
                return (this.bottom = $('.mode-footer__wrap').outerHeight(true))
            }
        }
    });

    //专业鼠标移入时展开科目
    $('.class-sort-menu').on('mouseover mouseout', '.class-sort-menu-li', function (e) {
        e = e || window.event;
        var that = $(this);
        if (that.find('.class-sort-second-menu__wrap').length == 0) return;
        if (e.type === 'mouseover') {
            if (!that.hasClass('active')) that.addClass('active');
        }
        if (e.type === 'mouseout') {
            if (that.hasClass('active')) that.removeClass('active');
        }
    });

    classesList(1, {});

    /**
     * 班级列表
     * @param pageNo
     * @param params 请求参数
     */
    function classesList(pageNo, params) {
        params = params || {};
        params.classesType =$('.classesType.active').val();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/web/classes/ajaxClassesList',
            contextId: 'classes-list',
            params: params,
            pageNo: pageNo,
            pageCall: classesList
        };
        getPageAjaxData(obj);
        lazyCourseImage();
    }

    // 专业/科目
    findSubjectList(function (data) {
        // 一级专业
        var parents = data['0'];
        var container = $('#myAffix > ul');
        if (parents && parents.length > 0) {
            parents.forEach(function (parent) {
                var clone = $('#myAffix > ul > li:eq(0)').clone().removeClass('hide');
                clone.children('a').html('<aside class="pull-right"><i class="fa fa-angle-right"></i></aside>' + parent.subjectName);
                container.append(clone);
                // 二级专业
                var sons = data['' + parent.id];
                clone.find('ul > li > section').append('<span class="cm-filter-item" data-subject="' + parent.id + '">全部</span>');
                if (sons && sons.length > 0) {
                    sons.forEach(function (son) {
                        clone.find('ul > li > section').append('<span class="cm-filter-item" data-subject="' + son.id + '">' + son.subjectName + '</span>');
                    })
                }
            });
        }
    });

    $('body').on('click', 'span[data-subject]', function () { // 按专业筛选班级
        var subjectIds = $(this).data('subject');
        classesList(1, {'subjectIds': subjectIds});
    }).on('click','.classesType',function(){

        $.each($('.classesType'), function( key, val ) {
            $(val).removeClass('active');

        } );
        $(this).addClass("active");
        classesList(1, {});
    });

    getAdvertList();

    /**
     *  加载广告图片
     */
    function getAdvertList() {
        $.ajax({
            url:baseloaction +'/web/classes/getAdvertList',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    var list = result.entity;
                    if(list !=null && list.length > 0){
                        var obj = list[0];

                        $(".class-banner").css('background-image','url('+obj.imageMap.url+')');

                        var pageUrl = obj.pageUrl;
                        var courseId = obj.courseId;
                        var redirectType = obj.redirectType;
                        var newPage = obj.newPage;
                        if(redirectType==2){//课程详情
                            pageUrl = baseloaction +'/web/course/courseInfo?courseId='+courseId;
                        }
                        var target = '_self';
                        if(newPage==2){
                            target = '_blank';
                        }
                        $(".class-banner").attr('onclick','window.open(\''+pageUrl+'\',\''+target+'\')');
                    }
                }
            }
        });
    }
});